const { getDbConfig, dbs } = require('./index');

const MSSqlHelper = require('./db/MSSQLHelper');
const MySQLHelper = require('./db/MySQLHelper');
const SQLiteHelper = require('./db/SQLiteHelper');

const sqlites = new Map();

module.exports = {
  dbs,
  getDbHelper (dbIndex) {
    let config = getDbConfig(dbIndex);

    if (config.type == 'sqlite' && config.name == 'inner-sqlite') {
      return global.sqlite;
    }

    let dbHelper = config ? (config.type == 'mysql' ? new MySQLHelper(config.dbIndex) : new MSSqlHelper(config.dbIndex)) : null;

    return dbHelper;
  },
  getSQLite (dbFile) {
    if (sqlites.get(dbFile)) {
      return sqlites.get(dbFile);
    }

    let instance = new SQLiteHelper(dbFile);

    sqlites.set(dbFile, instance);

    return instance;
  }
};